﻿@page "/credentialconfs/{id}/{action}"

@using SimpleIdServer.CredentialIssuer.Website.Stores.CredentialIssuer;
@inject IState<CredentialConfigurationState> credentialConfigurationState
@inject IDispatcher dispatcher
@inject NavigationManager navigationManager
@inherits Fluxor.Blazor.Web.Components.FluxorLayout

@if(!credentialConfigurationState.Value.IsLoading)
{
    <RadzenBreadCrumb>
        <RadzenBreadCrumbItem Path="/credentialconfs" Text="@Global.CredentialConfigurationsTitle" />
        <RadzenBreadCrumbItem Text="@credentialConfigurationState.Value.CredentialConfiguration.ServerId" />
    </RadzenBreadCrumb>

    <RadzenText class="mt-3 mb-3 no-margin" Text="@credentialConfigurationState.Value.CredentialConfiguration.ServerId" TextStyle="TextStyle.DisplayH3" />

    <RadzenCard Variant="Variant.Filled" class="mt-2">
        <RadzenTabs Change=@OnChange @bind-SelectedIndex="selectedIndex">
            <Tabs>
                <RadzenTabsItem Text="@Global.Details">
                    <CredentialConfigurationSettings CredentialConfiguration="@credentialConfigurationState.Value.CredentialConfiguration"></CredentialConfigurationSettings>
                </RadzenTabsItem>
                <RadzenTabsItem Text="@Global.Claims">
                    <CredentialConfigurationClaims CredentialConfiguration="@credentialConfigurationState.Value.CredentialConfiguration"></CredentialConfigurationClaims>
                </RadzenTabsItem>
                <RadzenTabsItem Text="@Global.Credentials">
                    <CredentialConfigurationInstances CredentialConfiguration=@credentialConfigurationState.Value.CredentialConfiguration></CredentialConfigurationInstances>
                </RadzenTabsItem>
            </Tabs>
        </RadzenTabs>
    </RadzenCard>
}
else
{
    <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.Center" Wrap="FlexWrap.Wrap" Class="rz-m-12" Gap="2rem">
        <RadzenProgressBarCircular ShowValue="true" Mode="ProgressBarMode.Indeterminate" Size="ProgressBarCircularSize.Large">
            <Template>@Global.Loading</Template>
        </RadzenProgressBarCircular>
    </RadzenStack>
}

@code {
    Dictionary<string, int> mappingActionNameToIndex = new Dictionary<string, int>
    {
        { "settings", 0 }, 
        { "claims", 1 },
        { "credentials", 2 }
    };

    [Parameter]
    public string id { get; set; } = null!;
    [Parameter]
    public string? action { get; set; } = null;

    int selectedIndex = 0;

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (string.IsNullOrWhiteSpace(id)) return;
        if (!string.IsNullOrWhiteSpace(action) && mappingActionNameToIndex.ContainsKey(action))
            selectedIndex = mappingActionNameToIndex[action];
        var act = new GetCredentialConfigurationAction { Id = id };
        dispatcher.Dispatch(act);
    }

    void OnChange(int index)
    {
        var kvp = mappingActionNameToIndex.Single(k => k.Value == index);
        navigationManager.NavigateTo($"/credentialconfs/{id}/{kvp.Key}");
    }
}